%%%-------------------------------------------------------------------
%%% File    : rdf_spalg2valg.erl
%%% Author  :  <vjache>
%%% Description : 
%%%
%%% Created :  4 May 2009 by  <vjache>
%%%-------------------------------------------------------------------
-module(rdf_spalg2valg).

-include("../include/rdf_spalg.hrl").  

%% API
-export([
		 init/0,
		 begin_visit_patt/2,
		 end_visit_patt/2,
		 visit_node/2,
		 begin_visit_bool/2,
		 end_visit_bool/2,
		 visit_pred_arg/2,
		 costify/1,
		 cost_of/1,
		 test/0
		 ]).
%BGP+Filter=Group
-record(bgp,
		{
		 node2quads=dict:new(),% dict Node->[Quad]
		 quad_cost =[],	% list [{Quad,Cost}]
		 var_pos =[],	% list [{Var,Pos}]
		 const_pos =[],	% list [{Const,Pos}]
		 filter=undefined 
		 }).

-record(state,
		{
		 groups=[]
		 }).



%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% Function: 
%% Description:
%%--------------------------------------------------------------------
init() ->
	{{left_bgp,right_bgp_list,filter},_LJoinList=[]}.

begin_visit_patt([Quad|_Root]=_Path,[Bgp|Tail]=_State) when element(1,Quad)==?QUAD_TAG_SA ->
	ok;

begin_visit_patt([Quad|_Root]=_Path,[Bgp|Tail]=_State) when element(1,Quad)==?QUAD_TAG_SA ->
	[Bgp#bgp{
			quad_cost=[{Quad,cost_of(Quad)}|Bgp#bgp.quad_cost]
		   }|Tail];
begin_visit_patt(_Context,State) ->
	State.

end_visit_patt([Patt|Tail]=_Context,State) ->
	io:format(shift("End pattern:~p~n",Tail), [element(1,Patt)]),
	State.

visit_node([{_Role,Node}|Tail]=_Context,State) ->
	io:format(shift("Node :~p~n",Tail), [Node]),
	State.


begin_visit_bool([BoolExpr|Tail]=_Context,State) ->
	io:format(shift("Begin bool expr:~p~n",Tail), [element(1,BoolExpr)]),
	State.

end_visit_bool([BoolExpr|Tail]=_Context,State) ->
	io:format(shift("End bool expr:~p~n",Tail), [element(1,BoolExpr)]),
	State.
visit_pred_arg([PredArg|Tail]=_Context,State) ->
	io:format(shift("Predicate arg:~p~n",Tail), [PredArg]),
	State.

shift(Ini,List) ->
	lists:foldl(fun(_E,Acc)->
						["\t"|Acc]
				end,Ini,List).


%%====================================================================
%% Internal functions
%%====================================================================
costify(QuadList) ->
	CQ=[{cost_of(Quad),Quad}||Quad<-QuadList],
	lists:sort(CQ).

cost_of({?QUAD_TAG_SA,S,P,O,G})->
	lists:min([cost_of(Node)||Node<-[{s,S},{p,P},{o,O},{g,G}]]);
cost_of({s,N}) when ?IS_CONST(N) ->
	{0.05,s};
cost_of({p,N}) when ?IS_CONST(N) ->
	{1,p};
cost_of({o,N}) when ?IS_CONST(N) ->
	{0.25,o};
cost_of({g,N}) when ?IS_CONST(N) ->
	{0.125,g};
cost_of({Role,N}) when ?IS_VAR(N) ->
	{10,Role}.

test() ->
	W=w,
	Alice=alice,
	AliceFoaf={data,"aliceFoaf"},
	Whom=whom,
	Ppd=ppd,
	Nick=nick,
	Mbox=mbox,
	 costify([
	  {?QUAD_TAG_SA,Alice,{foaf,"mbox"},{"mailto:alice@work.example"},AliceFoaf},
	  {?QUAD_TAG_SA,Alice,{foaf,"knows"},Whom,AliceFoaf},
	  {?QUAD_TAG_SA,Whom,{foaf,"mbox"},Mbox,AliceFoaf},
	  {?QUAD_TAG_SA,Whom,{rdfs,"seeAlso"},Ppd,AliceFoaf},
	  {?QUAD_TAG_SA,Ppd,{rdf,"type"},{foaf,"PersonalProfileDocument"},AliceFoaf},
	  {?QUAD_TAG_SA,W,{foaf,"mbox"},Mbox,Ppd},
	  {?QUAD_TAG_SA,W,{foaf,"nick"},Nick,Ppd},
	  {?QUAD_TAG_SA,{foaf,id_0},{foaf,"nick"},Nick,Ppd}
	 ]).

qlist_to_join(QuadList) ->
	ok.
	
